package com.example.myanimator.five;

import android.animation.Animator;
import android.animation.Keyframe;
import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewPropertyAnimator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myanimator.R;
import com.example.myanimator.evaluator.CharEvaluator;
import com.example.myanimator.evaluator.FallingBallEvaluator;
import com.example.myanimator.evaluator.FallingImageView;
import com.example.myanimator.evaluator.ReverseEvaluator;
import com.example.myanimator.view.CircleImageView;

public class FiveActivity extends AppCompatActivity {

    private TextView tv2;
    private TextView tv_3;
    private CircleImageView circleImageView;
    private FallingImageView fallingImageView;
    private Animator animator;
    private Animator animator1;
    private LinearLayout llContainer;
    private int index = 0;
    private LayoutTransition layoutTransition;
    private ObjectAnimator animin;
    private ObjectAnimator animOut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_five);
        tv2 = findViewById(R.id.tv_2);
        tv_3 = findViewById(R.id.tv_3);
        circleImageView = findViewById(R.id.leke_group_space_civ_header);
        fallingImageView = findViewById(R.id.fall_image);
        llContainer = findViewById(R.id.ll_container);

        initEva1();
        initEva2();


        findViewById(R.id.tv_5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animator.start();
                animator1.start();
                fallingImageView.animate().setDuration(5000).translationX(500).translationY(500).alpha(1).alpha(0).alpha(1);
                if (index > 0) {
                    llContainer.getChildAt(--index).setVisibility(View.GONE);
                }
            }
        });


        /**给viewgroup添加LayoutTransition  那么里面的子view 出现消失 会执行动画*/

        layoutTransition = new LayoutTransition();

        //出场动画 view 显示时的动画
        animin = ObjectAnimator.ofFloat(null, "translationX", 0f, 100f, 0f);
        layoutTransition.setAnimator(LayoutTransition.APPEARING, animin);
        layoutTransition.setDuration(LayoutTransition.APPEARING,5000);
        layoutTransition.setInterpolator(LayoutTransition.APPEARING,new AccelerateInterpolator());
        //入场动画： view 在这个容器中消失时触发的
        animOut = ObjectAnimator.ofFloat(null, "rotation", 0f, 90f, 0f);
        layoutTransition.setAnimator(LayoutTransition.DISAPPEARING, animOut);
        llContainer.setLayoutTransition(layoutTransition);

        findViewById(R.id.tv_6).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addView();

            }
        });
        findViewById(R.id.tv_7).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeView();

            }
        });


    }

    private void addView() {

        /**
         * 其中 int transition Type 表示当前应用动画 的对象范围，取值如下。
         • APPEARING ：元素在容器中出现时所定义的动画。
         • DISAPPEARING 元素在容器中消失时所定义的动画
         • CHANGE APPEARING 由于容器中要显现 个新的元素 需要变化的元素所应
         用的动
         • CHANGE DISAPPEARING 当容器中某个元素消失时 其他需要变化的元素所应用
         的动画。
         Animator nimator 表示当前所选范围的控件所使用的动画

         ＊setStagger : 针对单个 Type 置每个子 item 动画的 时间间隔
            public void setStagger(int transitionType, long duration)
         */

        TextView button = new TextView(this);
        button.setTextColor(Color.WHITE);
        button.setText("” button” + " + index);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.
                LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
        llContainer.addView(button, index);
        index++;
    }

    private void removeView() {
        if (index > 0 && llContainer.getChildCount() > 0) {
            llContainer.removeViewAt(0);
        }
    }


    private void initEva1() {
        Keyframe frame0 = Keyframe.ofFloat(0f, 0);
        Keyframe frame1 = Keyframe.ofFloat(0.1f, -360f);
        frame1.setInterpolator(new BounceInterpolator());
        Keyframe frame2 = Keyframe.ofFloat(0.2f, 360f);
        frame2.setInterpolator(new LinearInterpolator());
        Keyframe frame3 = Keyframe.ofFloat(0.3f, -20f);
        Keyframe frame4 = Keyframe.ofFloat(0.4f, 20f);
        Keyframe frame5 = Keyframe.ofFloat(0.5f, -20f);
        Keyframe frame6 = Keyframe.ofFloat(0.6f, 20f);
        Keyframe frame7 = Keyframe.ofFloat(0.7f, -20f);
        Keyframe frame8 = Keyframe.ofFloat(0.8f, 20f);
        Keyframe frame9 = Keyframe.ofFloat(0.9f, -20f);
        Keyframe frame10 = Keyframe.ofFloat(1, 0);
        PropertyValuesHolder frameHolder = PropertyValuesHolder.ofKeyframe("rotation",
                frame0, frame1, frame2, frame3, frame4, frame5, frame6, frame7, frame8, frame9, frame10);
        animator = ObjectAnimator.ofPropertyValuesHolder(circleImageView, frameHolder);
        animator.setDuration(5000);

    }

    private void initEva2() {
        Keyframe frame0 = Keyframe.ofObject(0f, new Point(0, 0));
        Keyframe frame1 = Keyframe.ofObject(0.1f, new Point(500, 500));
        Keyframe frame10 = Keyframe.ofObject(1, new Point(0, 1000));

        PropertyValuesHolder frameHolder = PropertyValuesHolder.ofKeyframe("FallingPos",
                frame0, frame1, frame10);
        frameHolder.setEvaluator(new FallingBallEvaluator());
        animator1 = ObjectAnimator.ofPropertyValuesHolder(fallingImageView, frameHolder);
        animator1.setDuration(5000);
    }
}